package pers.ervinse.shoppingmall.home.fragment;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import pers.ervinse.shoppingmall.BaseFragment;
import pers.ervinse.shoppingmall.R;
import pers.ervinse.shoppingmall.home.adapter.HomeAdapter;
import pers.ervinse.shoppingmall.domain.Goods;
import pers.ervinse.shoppingmall.utils.OkhttpUtils;
import pers.ervinse.shoppingmall.utils.PropertiesUtils;

/**
 * 首页碎片
 */
public class HomeFragment extends BaseFragment {

    private static final String TAG = HomeFragment.class.getSimpleName();
    private Handler handler = new Handler();

    private RecyclerView rvHome;
    private ImageView ib_top;
    private TextView tv_search_home;
    private TextView tv_message_home;

    private HomeAdapter adapter;
    private ViewPager2 viewPager2;  // 轮播图的 ViewPager2
    private SliderAdapter sliderAdapter;  // 轮播图适配器

    List<Goods> goodsList;
    List<String> imageUrls;  // 轮播图图片 URL 列表

    /**
     * 初始化视图
     * @return
     */
    @Override
    public View initView() {
        Log.i(TAG, "主页视图初始化");
        View view = View.inflate(mContext, R.layout.fragment_home, null);
        rvHome = view.findViewById(R.id.rv_home);
        tv_search_home = view.findViewById(R.id.tv_search_home);
        tv_message_home = view.findViewById(R.id.tv_message_home);
        viewPager2 = view.findViewById(R.id.slider_viewpager);  // 获取轮播图控件
        return view;
    }

    /**
     * 初始化数据
     */
    public void initData() {
        super.initData();
        Log.i(TAG, "主页数据初始化");

        new Thread() {
            @Override
            public void run() {
                Log.i(TAG, "进入获取商品线程");

                Gson gson = new Gson();
                String responseJson = null;
                try {
                    // 发送获取商品请求
                    String url = PropertiesUtils.getUrl();
                    responseJson = OkhttpUtils.doGet(url + "/goods/getHotGoods");
                    Log.i(TAG, "获取热点商品响应json:" + responseJson);
                    goodsList = gson.fromJson(responseJson, new TypeToken<List<Goods>>() {
                    }.getType());
                    Log.i(TAG, "获取热点响应解析对象:" + goodsList);

                    // 获取轮播图数据 (假设是从服务器返回的图片列表)
                    imageUrls = new ArrayList<>();
                    imageUrls.add("http://47.109.159.9:4200/file/lunbo/1.webp");
                    imageUrls.add("http://47.109.159.9:4200/file/lunbo/2.jpg");
                    imageUrls.add("http://47.109.159.9:4200/file/lunbo/3.jpg");

                    // 切回主线程加载轮播图
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            // 为轮播图设置适配器
                            sliderAdapter = new SliderAdapter(imageUrls);
                            viewPager2.setAdapter(sliderAdapter);

                            // 创建网格布局
                            adapter = new HomeAdapter(goodsList, mContext);
                            rvHome.setAdapter(adapter);
                            GridLayoutManager manager = new GridLayoutManager(mContext, 2);
                            rvHome.setLayoutManager(manager);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                    Looper.prepare();
                    Toast.makeText(mContext, "获取数据失败,服务器错误", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }
        }.start();
    }

    /**
     * 刷新数据
     */
    @Override
    public void refreshData() {
        Log.i(TAG, "联网刷新数据");
        new Thread() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void run() {
                Log.i(TAG, "进入获取商品线程");

                Gson gson = new Gson();
                String responseJson = null;
                try {
                    // 发送获取商品请求
                    String url = PropertiesUtils.getUrl();
                    responseJson = OkhttpUtils.doGet(url + "/goods");
                    Log.i(TAG, "获取商品响应json:" + responseJson);
                    goodsList = gson.fromJson(responseJson, new TypeToken<List<Goods>>() {
                    }.getType());
                    Log.i(TAG, "获取商品响应解析对象:" + goodsList);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            // 防止因为初始化未加载布局
                            if (adapter != null) {
                                adapter.setGoodsList(goodsList);
                                adapter.flushView();
                            }
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                    Looper.prepare();
                    Toast.makeText(mContext, "获取数据失败,服务器错误", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }
        }.start();
    }

    @Override
    public void saveData() {
    }
}
